﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
using System.Drawing;

/// <summary>
///clsParseCode 的摘要说明
/// </summary>
/// 
namespace uMESExternalControl.ToleranceInputLib
{
    public class clsParseCode
    {
        public DataTable dt;
        public clsParseCode()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
            dt = new DataTable();
            dt.Columns.Add("strHtml");
            dt.Columns.Add("strFileName");
            DataRow dr;
            dr = dt.NewRow();
            dr[0] = "<+>";
            dr[1] = "start3.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<&1>";
            dr[1] = "zhixiandu.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<&2>";
            dr[1] = "pinmiandu.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<&3>";
            dr[1] = "yuandu.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<&4>";
            dr[1] = "yuanzhudu.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<&5>";
            dr[1] = "xianlunkuo.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<&6>";
            dr[1] = "mianlunkuo.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<&7>";
            dr[1] = "qinxiedu.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<&8>";
            dr[1] = "cuizhidu.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<&9>";
            dr[1] = "pinxindu.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<&10>";
            dr[1] = "weizhidu.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<&11>";
            dr[1] = "axis.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<&12>";
            dr[1] = "duichendu.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<&13>";
            dr[1] = "yuantiaodong.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<&15>";
            dr[1] = "quantiaodong.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<O>";
            dr[1] = "diameter.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<SO>";
            dr[1] = "qiuzhijin.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<M>";
            dr[1] = "M.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<E>";
            dr[1] = "L.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<S>";
            dr[1] = "S.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<&P>";
            dr[1] = "P.png";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "<&S>";
            dr[1] = "ST.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<&T>";
            dr[1] = "T.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<&F>";
            dr[1] = "F1.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<&E>";
            dr[1] = "E1.png";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "<#C>";
            dr[1] = "C.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<#B>";
            dr[1] = "B.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<#D>";
            dr[1] = "D.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<#E>";
            dr[1] = "E.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<#G>";
            dr[1] = "G.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<#F>";
            dr[1] = "f.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<#h>";
            dr[1] = "h.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<$t>";
            dr[1] = "zhfu.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<$S>";
            dr[1] = "du.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<J>";
            dr[1] = "J.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<(>";
            dr[1] = "(.png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "<)>";
            dr[1] = ").png";
            dt.Rows.Add(dr);
            dr = dt.NewRow();


        }

        // struct Image

        //public String ParseImageCode(String strImageHtml)
        //{

        //    try
        //    {
        //        ArrayList str;


        //        String strImageHtml, strMid, strUp, strDown, strImageHtmlTemp, strHtml;
        //        int intFlag, intFlag2, intStart, intEnd, intImageIndex;
        //        //strImageHtml = txtHtml.Text.Trim();
        //        clsDrawImage oDraw = new clsDrawImage();
        //        intStart = strImageHtml.IndexOf("<Image>");
        //        intEnd = strImageHtml.IndexOf("</Image>");
        //        strMid = "";
        //        strUp = "";
        //        strDown = "";
        //        if (intStart > -1)
        //        {
        //            strImageHtmlTemp = strImageHtml.Substring(intStart + 7, intEnd - intStart - 7);
        //            intFlag = strImageHtmlTemp.IndexOf("<T");
        //            if (intFlag > -1)
        //            {
        //                //strImageHtml = strImageHtmlTemp.Substring(0, strImageHtmlTemp.Length - 1);
        //                strMid = strImageHtmlTemp.Substring(0, intFlag);
        //                intFlag2 = strImageHtmlTemp.IndexOf("!");
        //                strUp = strImageHtmlTemp.Substring(intFlag + 2, intFlag2 - intFlag - 2);
        //                strDown = strImageHtmlTemp.Substring(intFlag2 + 1, strImageHtmlTemp.Length - intFlag2 - 2);
        //            }
        //            else
        //            {
        //                intFlag = strImageHtmlTemp.IndexOf("<H>");
        //                if (intFlag > -1)
        //                {
        //                    intFlag2 = strImageHtmlTemp.IndexOf("</H>");
        //                    strMid = strImageHtmlTemp.Substring(0, intFlag);
        //                    strUp = strImageHtmlTemp.Substring(intFlag + 3, intFlag2 - intFlag - 3);
        //                    strDown = "";
        //                }
        //                else
        //                {
        //                    intFlag = strImageHtmlTemp.IndexOf("<L>");
        //                    if (intFlag > -1)
        //                    {
        //                        intFlag2 = strImageHtmlTemp.IndexOf("</L>");
        //                        strMid = strImageHtmlTemp.Substring(0, intFlag);
        //                        strUp = "";
        //                        strDown = strImageHtmlTemp.Substring(intFlag + 3, intFlag2 - intFlag - 3);

        //                    }
        //                    else
        //                    {
        //                        strMid = strImageHtmlTemp;
        //                        strUp = "";
        //                        strDown = "";
        //                    }
        //                }

        //            }
        //        }
        //        intImageIndex = (int)Session["intMaxImageIndex"] + 1;
        //        Session["intMaxImageIndex"] = intImageIndex;
        //        strHtml = "";
        //        if (strMid != "")
        //        {
        //            strHtml = oDraw.DrawTolerance(strMid, strUp, strDown, intImageIndex);
        //            ftbMyTest.Text += strHtml;

        //            //string fieldId = Guid.NewGuid().ToString();

        //            //string field = strHtml;

        //            //btnTest.Attributes.Add("OnClick", "insertValue('" + fieldId + "','" + field + "')");
        //        }

        //    }
        //    catch (Exception ex)
        //    {
        //        lblMessage.Text = ex.Message;
        //    }
        //    finally
        //    {
        //    }
        //}

        //将图片Html转换成后台编码 
        public String ParseImageCode(String strImageHtml)
        {
            int i, intRowCount;
            String strHtmlTemp;
            try
            {
                intRowCount = dt.Rows.Count;
                for (i = 0; i < intRowCount; i++)
                {
                    strHtmlTemp = dt.Rows[i][1].ToString();
                    if (strImageHtml.IndexOf(@"/" + strHtmlTemp) > -1)
                    {
                        return dt.Rows[i][0].ToString();

                    }

                }
                return "";

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {

            }
        }

        //将后台编码转换成图片名称 
        public String ParseImageFile(String strImageCode)
        {
            int i, intRowCount, intStart, intEnd;
            String strImageHtml, strFileName;
            try
            {
                intRowCount = dt.Rows.Count;
                for (i = 0; i < intRowCount; i++)
                {
                    if (dt.Rows[i][0].ToString() == strImageCode)
                    {
                        strFileName = dt.Rows[i][1].ToString();
                        return strFileName;

                    }

                }
                return "";

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {

            }
        }

        /// <summary>
        /// strPreviewCode转化为可显示的strPreviewCodeDis
        /// </summary>
        /// <param name="strPreviewCode"></param>
        /// <param name="strImageGetPath">配置文件的路径</param>
        /// <param name="strImageTempPath">配置文件的路径</param>
        /// <param name="strPreviewCodeDis"></param>
        public void ParseCode(String strPreviewCode, string strImageGetPath, string strImageTempPath, ref string strPreviewCodeDis)
        {
            try
            {
                String strPreviewCodeTemp, strHtmlStripped, strTemp, strCodeTemp;
                String strFileDoc, strFileTemp, strFilePath, strFileName, strMapPath, strHtml;
                int intStartFlag, intEndFlag, intFlag1, intFlag2, intCodeStartFlag, intCodeEndFlag;
                String strImageIndex;
                clsParseCode oParse = new clsParseCode();
                Bitmap objBmp;
                String strLeft, strUp, strDown, strRight;

                strPreviewCode = strPreviewCode.Trim().Replace("\r\n\t\t", "");
                strPreviewCode = strPreviewCode.Trim().Replace("<P>", "");
                strPreviewCode = strPreviewCode.Trim().Replace("</P>", "");
                strPreviewCode = strPreviewCode.Replace("\"", "'");

                intStartFlag = strPreviewCode.IndexOf("<Image>");
                intEndFlag = strPreviewCode.IndexOf("</Image>");
                strHtmlStripped = "";
                strHtml = "";

                //strMapPath = MapPath("~");
                //strFileTemp = strMapPath + @"\Images\";

                //clsCon oCon = new clsCon();
                String strImagePath = strImageGetPath;

                //strImagePath =  ConfigurationManager.AppSettings["ImageGetPath"].ToString();

                strFileTemp = strImagePath;

                uMESExternalControl.ToleranceInputLib.clsDrawImage oDraw = new uMESExternalControl.ToleranceInputLib.clsDrawImage();
                DataTable dtImage = new DataTable();

                while (intStartFlag > -1)
                {
                    if (intStartFlag > 0)
                    {
                        //将纯文本输出
                        strHtml = strPreviewCode.Substring(0, intStartFlag);
                        strPreviewCode = strPreviewCode.Substring(intStartFlag);
                        strPreviewCodeDis += strHtml;
                        intStartFlag = strPreviewCode.IndexOf("<Image>");
                        intEndFlag = strPreviewCode.IndexOf("</Image>");
                        continue;
                    }

                    else
                    {
                        strHtml = "";
                        strPreviewCodeTemp = strPreviewCode.Substring(intStartFlag + 7, intEndFlag - intStartFlag - 7);
                        intCodeStartFlag = strPreviewCodeTemp.IndexOf("<&70><+>");
                        intCodeEndFlag = strPreviewCodeTemp.IndexOf("<+><&90>");

                        if (intCodeStartFlag > -1)
                        {
                            //代码为行位公差时
                            strCodeTemp = strPreviewCodeTemp.Replace("<&70><+>", "");
                            strCodeTemp = strCodeTemp.Replace("<+><&90>", "");

                            intFlag1 = strCodeTemp.IndexOf("<");
                            intFlag2 = strCodeTemp.IndexOf(">");
                            while (intFlag1 > -1)
                            {
                                strTemp = strCodeTemp.Substring(intFlag1, intFlag2 - intFlag1 + 1);
                                strCodeTemp = strCodeTemp.Replace(strTemp, "");
                                intFlag1 = strCodeTemp.IndexOf("<");
                                intFlag2 = strCodeTemp.IndexOf(">");
                            }

                            strHtmlStripped = strCodeTemp;
                            //strPreviewCodeTemp = strPreviewCode;
                            strPreviewCodeTemp = strPreviewCodeTemp.Replace(@"<Image>", "");
                            strPreviewCodeTemp = strPreviewCodeTemp.Replace(@"</Image>", "");

                            objBmp = oDraw.DrawShapeTolerance(strPreviewCodeTemp, strHtmlStripped, strFileTemp);
                        }
                        else
                        {
                            intCodeStartFlag = strPreviewCodeTemp.IndexOf("<T");
                            intCodeEndFlag = strPreviewCodeTemp.IndexOf("!");
                            if (intCodeStartFlag > -1)
                            {
                                //代码为上下标公差

                                strLeft = strPreviewCodeTemp.Substring(0, intCodeStartFlag);
                                strUp = strPreviewCodeTemp.Substring(intCodeStartFlag + 2, intCodeEndFlag - intCodeStartFlag - 2);
                                strDown = strPreviewCodeTemp.Substring(intCodeEndFlag + 1, strPreviewCodeTemp.Length - intCodeEndFlag - 2);
                                objBmp = oDraw.DrawTolerance(strLeft, strUp, strDown);
                            }
                            else
                            {
                                intCodeStartFlag = strPreviewCodeTemp.IndexOf("<H>");
                                intCodeEndFlag = strPreviewCodeTemp.IndexOf("</H>");
                                if (intCodeStartFlag > -1)
                                {
                                    //代码为只有上公差
                                    strLeft = strPreviewCodeTemp.Substring(0, intCodeStartFlag);
                                    strUp = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3);
                                    strDown = "";
                                    objBmp = oDraw.DrawTolerance(strLeft, strUp, strDown);
                                }
                                else
                                {
                                    intCodeStartFlag = strPreviewCodeTemp.IndexOf("<L>");
                                    intCodeEndFlag = strPreviewCodeTemp.IndexOf("</L>");
                                    if (intCodeStartFlag > -1)
                                    {
                                        //代码为只有下公差
                                        strLeft = strPreviewCodeTemp.Substring(0, intCodeStartFlag);
                                        strUp = "";
                                        strDown = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3);
                                        objBmp = oDraw.DrawTolerance(strLeft, strUp, strDown);
                                    }
                                    else
                                    {
                                        intCodeStartFlag = strPreviewCodeTemp.IndexOf("<√>");
                                        intCodeEndFlag = strPreviewCodeTemp.IndexOf("</√>");
                                        if (intCodeStartFlag > -1)
                                        {
                                            //代码为指定加工方法时
                                            strLeft = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3);

                                            strRight = strPreviewCodeTemp.Substring(intCodeEndFlag + 4);
                                            objBmp = oDraw.DrawRoughness(strLeft, strRight);
                                        }
                                        else
                                        {
                                            intCodeStartFlag = strPreviewCodeTemp.IndexOf("<R>");
                                            intCodeEndFlag = strPreviewCodeTemp.IndexOf("</R>");
                                            if (intCodeStartFlag > -1)
                                            {
                                                //代码为去除材料时
                                                strLeft = strPreviewCodeTemp.Substring(intCodeStartFlag + 3, intCodeEndFlag - intCodeStartFlag - 3);

                                                strRight = "";
                                                objBmp = oDraw.DrawRoughness(strLeft, strRight);
                                            }
                                            else
                                            {
                                                intCodeStartFlag = strPreviewCodeTemp.IndexOf("<Q>");
                                                intCodeEndFlag = strPreviewCodeTemp.IndexOf("</Q>");
                                                if (intCodeStartFlag > -1)
                                                {
                                                    //代码为不去除材料时
                                                    strLeft = "";

                                                    strRight = "";
                                                    objBmp = oDraw.DrawRoughness(strLeft, strRight);
                                                }
                                                else
                                                {
                                                    objBmp = oDraw.DrawSpecImgOrString(strPreviewCodeTemp, strFileTemp); 
                                                }
                                            }
                                        }
                                    }
                                }

                            }
                        }

                        //保存
                        if (objBmp != null)
                        {
                            
                            strFileDoc = strImageTempPath;
                            //if (Session["intMaxImageIndex"] == null)
                            //{
                            //    Session["intMaxImageIndex"] = 0;
                            //    intImageIndex = 1;
                            //}
                            //else
                            //{
                            //    intImageIndex = (int)Session["intMaxImageIndex"] + 1;
                            //}

                            strImageIndex = System.DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff");
                            strFileName = "ImageTemp-" + strImageIndex + ".png";
                            //strFileName = "ImageTemp-" + intImageIndex + ".png";
                            strFilePath = strFileDoc + strFileName;
                            //System.Threading.Thread.Sleep(1);
                            //System.IO.FileInfo oFile = new System.IO.FileInfo(strFilePath);
                            //while (oFile.Exists == true)
                            //{
                            //    intImageIndex += 1;
                            //    strFileName = "ImageTemp-" + intImageIndex + ".png";
                            //    strFilePath = strFileDoc + strFileName;
                            //    oFile = new System.IO.FileInfo(strFilePath);
                            //}

                            System.IO.FileInfo oFile = new System.IO.FileInfo(strFilePath);
                            while (oFile.Exists == true)
                            {
                                strImageIndex = strImageIndex + "1";
                                strFileName = "ImageTemp-" + strImageIndex + ".png";
                                strFilePath = strFileDoc + strFileName;
                                oFile = new System.IO.FileInfo(strFilePath);
                            }

                            objBmp.Save(strFilePath);

                            strPreviewCodeDis = strFileName;
                        }

                    }
                    if (intEndFlag + 8 < strPreviewCode.Length)
                    {
                        strPreviewCode = strPreviewCode.Substring(intEndFlag + 8);
                        intStartFlag = strPreviewCode.IndexOf("<Image>");
                        intEndFlag = strPreviewCode.IndexOf("</Image>");
                    }
                    else
                    {
                        intStartFlag = -1;
                    }

                }
                intStartFlag = strPreviewCode.IndexOf("<Image>");
                if (strPreviewCode != "" && intStartFlag == -1)
                {
                    strPreviewCodeDis += strPreviewCode;
                }
            }
            catch (Exception myError)
            {

            }

        }
    }

}